//index.js
const app = getApp()

Page({
  data: {
    avatarUrl:"",
    nickName:"",
    myBrandid:"",
    myLevel:"",
    myCompanyName:"",
    brandid:"",
    level:"",
    subordinate:[],
    companyName:"",
    logged: false,
    isShowBind:false,
    isShowStep:false,
    isShowInfor:true,
    phoneNumber:"",
    numList: [{
      name: '小程序授权'
    }, {
      name: '电话授权'
    }, {
      name: '绑定我的身份'
    }, {
      name: '绑定推荐码'
    }, ],
    num: 0,
  },

  onLoad: function() {
    var that=this;
    console.log(that.data.brandid)
    setTimeout(()=>{
      wx.cloud.callFunction({
        name: 'login',
        data: {
        }
      }).then(res => {
        if(res.result.data.length>0){
          console.log('res: ', res)
          let userInfor= res.result.data[0]
          app.globalData.logged=true;
          app.globalData.avatarUrl=userInfor.avatarUrl;
          app.globalData.nickName=userInfor.nickName;
          app.globalData.brandid=userInfor.brandid;
          app.globalData.level=userInfor.level;
          app.globalData.companyName=userInfor.companyName;
          app.globalData.phoneNumber=userInfor.phoneNumber;
          app.globalData.myBrandid=userInfor.myBrandid,
          app.globalData.myLevel=userInfor.myLevel,
          app.globalData.myCompanyName=userInfor.myCompanyName,
          that.setData({
            myBrandid:app.globalData.myBrandid,
            myLevel:app.globalData.myLevel,
            myCompanyName:app.globalData.myCompanyName,
            logged:app.globalData.logged,
            avatarUrl:app.globalData.avatarUrl,
            nickName:app.globalData.nickName,
            brandid:app.globalData.brandid,
            level:app.globalData.level,
            companyName:app.globalData.companyName,
            phoneNumber:app.globalData.phoneNumber
          })
        }else{
          that.setData({
            isShowBind:true
          })
        }
      }) 
    },1000)
  },
  addtest(){
    var that=this;
    wx.scanCode({
      success(res) {
        var result=JSON.parse(res.result)
        var device_logicid=result.device_logicid
        var gprs_number=result.gprs_number
        wx.cloud.callFunction({
          name: 'scanCode',
          data: {
            device_logicid,
            gprs_number,
            level:1,
            brandid:that.data.brandid
          }
        }).then(res => {
          console.log(res)
        }) 
      }})
// wx.cloud.callFunction({
//       name: 'addSubordinate',
//       data: {
//         brandid:that.data.brandid
//       }
//     }).then(res => {
//       console.log(res)
//     }) 
  },
  showBind(){
    this.setData({
      isShowStep:true
    })
  },
  qux(){
    this.setData({
      isShowInfor:false
    })
  },
  subStep(){
    this.setData({
      isShowInfor:true
    })
  },
  nextStep() {
    this.setData({
      num: this.data.num + 1
    })
  },
  previousStep() {
    this.setData({
      num: this.data.num - 1
    })
  },
  onGetUserInfo: function(e) {
    console.log(e.detail)
    if (!this.data.logged && e.detail.userInfo) {
      this.setData({
        avatarUrl: e.detail.userInfo.avatarUrl,
        nickName: e.detail.userInfo.nickName
      })
    }
  },
  getPhoneNumber(e){
    const that = this
    console.log(JSON.stringify(e));
    wx.cloud.callFunction({
      name: 'getPhone',
      data: {
        action:'getcellphone',
        id:e.detail.cloudID
      }
    }).then(res => {
      console.log('res: ', res.result.phoneNumber)
      that.setData({
        phoneNumber:res.result.phoneNumber
      })
    }) 
  },
  myScanBindCode() {
    const that = this
    wx.scanCode({
      success(res) {
        console.log(res)
        var results= res.result.split(",")
        if(results[0].split("_")[0].indexOf("shuwh#4567CSAW#68*")){
          that.setData({
            myBrandid:results[1],
            myLevel:results[2],
            myCompanyName:results[3],
          })
        }else{
          wx.showModal({
            title: '提示',
            content: '此二维码不是本公司二维码请重新扫码',
            success (res) {
              if (res.confirm) {
                that.myScanBindCode()
              } else if (res.cancel) {
              }
            }
          })
        }
        // that.register()
      }})
    },
    scanBindCode() {
      const that = this
      wx.scanCode({
        success(res) {
          var results= res.result.split(",")
          if(results[0].split("_")[0].indexOf("shuwh#4567CSAW#68*")){
            if(that.data.myBrandid==results[1]){
              wx.showModal({
                title: '提示',
                content: '两次扫码的二维码不可相同',
                success (res) {
                  if (res.confirm) {
                    that.scanBindCode()
                  } else if (res.cancel) {
                  }
                }
              })
            }else{
              that.setData({
                brandid:results[1],
                level:results[2],
                companyName:results[3],
              })
            }

          }else{
            wx.showModal({
              title: '提示',
              content: '此二维码不是本公司二维码请重新扫码',
              success (res) {
                if (res.confirm) {
                  that.scanBindCode()
                } else if (res.cancel) {
                }
              }
            })
          }
        }})
      },

    toList(){
      wx.navigateTo({
        url: '/pages/list/list',
      })
    },
    register(){
      const that = this
      console.log("register")
      wx.cloud.callFunction({
        name: 'register',
        data: {
          avatarUrl:that.data.avatarUrl,
          nickName:that.data.nickName,
          phoneNumber:that.data.phoneNumber,
          myBrandid:that.data.myBrandid,
          myLevel:that.data.myLevel,
          myCompanyName:that.data.myCompanyName,
          brandid:that.data.brandid,
          level:that.data.level,
          companyName:that.data.companyName,
        }
      }).then(res => {
        console.log('res: ', res)
        that.setData({
          logged:true
        })
      }) 
    },
    toScanCode(){
      wx.navigateTo({
        url: '/pages/scanCode/scanCode',
      })
    },
    scanDeviceCode() {
      const that = this
      wx.scanCode({
        success(res) {
          var result=JSON.parse(res.result)
          var device_logicid=result.device_logicid
          var gprs_number=result.gprs_number
          if(that.data.subordinate.indexOf(device_logicid.toString())>=0){
            wx.cloud.callFunction({
              name: 'findScanCodeById',
              data: {
                device_logicid,
                level:that.data.myLevel,
                brandid:that.data.myBrandid
              }
            }).then(res => {
              if(res.result.data.length>0){
                wx.showModal({
                  title: '提示',
                  content: '此设备已扫码过无需二次扫描',
                })
              }else{
                wx.cloud.callFunction({
                  name: 'scanCode',
                  data: {
                    device_logicid,
                    gprs_number,
                    level:that.data.myLevel,
                    brandid:that.data.myBrandid
                  }
                }).then(res => {
                  console.log(res)
                  if(res.result.stats.updated>0){
                    wx.showToast({
                      title: '提交成功',
                      icon:'success'
                    })
                  }
                }) 
              }
            }) 
          }else{
            wx.showModal({
              title: '提示',
              content: '设备不存在请重新扫码',
            })
          }
        }})
      },
})
